<!-- 资金下拨 -->
<template>
    <div class='' v-loading="loading" element-loading-text="数据加载中..." element-loading-spinner="el-icon-loading">


        <el-form ref="ruleForm" class="passForm" label-position="left" label-width="100px" :model="form" :rules="rules">
            <el-form-item label="下拨账户">
                <el-input v-model="dexinBankNumber" readonly>
                </el-input>
            </el-form-item>
            <el-form-item label="子公司账户" prop="source">
                <el-select v-model="form.source" placeholder="选择下拨公司" @change="selectChange" :filterable="true">
                    <el-option v-for="(item, index) in companyList" :key="item.bank_sub_account + '_' + index"
                        :label="item.company_name" :value="item.bank_sub_account"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="子账号" class="red-color">
                <el-input v-model="subAccount" readonly placeholder="请先选择子账户">
                </el-input>
            </el-form-item>
            <el-form-item label="账户余额" class="red-color">
                <el-input v-model="money" readonly>
                </el-input>
            </el-form-item>
            <el-form-item label="转账金额" prop="transfer_amount" class="red-color">
                <el-input-number v-model="form.transfer_amount" class="red" controls-position="right" :min="0"
                    :max="money">
                </el-input-number>
                <span style="margin-left: 10px;">元</span>
            </el-form-item>
            <el-form-item class="passBtn">
                <el-button class="btn" type="primary" @click="surePay">确认转账</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { getAccountMoney, accountDown, getCompanyAccount } from '@/api/businessUser/moneyManagment';
import { dexinBankNumber } from '@/utils/tool';
export default {
    //import引入的组件需要注入到对象中才能使用
    components: {},
    props: ['inList'],
    data() {
        //这里存放数据
        const numCheck = (rule, value, callback) => {
            // 我这里不是必填的，如果是必填的再判断一下value === ''
            let capital = /^[0-9.]*$/;
            if (!capital.test(value)) {
                callback(new Error(`请输入整数或小数`));
            } else {
                let data = JSON.stringify(value)
                let arr = data.split('')
                let mix = data.indexOf('.')
                if (mix != -1) {
                    if (mix == 0 || mix == arr.length - 1) {
                        callback(new Error(`小数点不能在第一位或最后一位`));
                        return
                    }
                    let dianNum = arr.filter((x) => x == ".");
                    if (dianNum.length > 1) {
                        callback(new Error(`小数点只能有一个`));
                        return
                    }
                    callback();
                } else {
                    callback();
                }
            }
        }
        return {
            loading: false,
            dexinBankNumber,
            form: {
                transfer_amount: 0,//上拨金额
                source: '',
            },
            money: 0,
            rules: {
                'transfer_amount': [{ validator: numCheck, trigger: 'blur' }],
                'source': [{ required: true, message: '请选择公司', trigger: 'change' }]
            },
            companyList: [],
            subAccount: "",//子账号的显示
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
        async getAccountMoney() {
            this.loading = true;
            let res = await getAccountMoney({ id: this.inList[0].id })
            this.loading = false;
            if (res.code == 200) {
                this.money = Number(Number(res.data.money).toFixed(2));
            }
        },
        async getCompanyAccount() {
            this.loading = true;
            let res = await getCompanyAccount();
            this.loading = false;
            if (res.code == 200) {
                this.companyList = res.data;
            }
        },
        surePay() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    if (this.form.transfer_amount <= 0) {
                        this.$message.error('请输入正确的金额!')
                        return;
                    } else if (this.form.money > this.form.transfer_amount) {
                        this.$message.error('账户余额不足!')
                        return;
                    }
                    this.$confirm('确定要向该账户进行资金拨动吗？', '系统提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(async () => {
                        let res = await accountDown(this.form)
                        if (res.code == 200) {
                            this.$message.success(res.msg)
                        } else {
                            this.$message.error(res.msg)
                        }
                        this.$emit('closeDialog')
                    }).catch(() => { });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        selectChange(val) {
            this.subAccount = val;
        }
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {

    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
        this.getAccountMoney();
        this.getCompanyAccount();
    },
    beforeCreate() { }, //生命周期 - 创建之前
    beforeMount() { }, //生命周期 - 挂载之前
    beforeUpdate() { }, //生命周期 - 更新之前
    updated() { }, //生命周期 - 更新之后
    beforeDestroy() { }, //生命周期 - 销毁之前
    destroyed() { }, //生命周期 - 销毁完成
    activated() { }, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.alert-box {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-bottom: 10px;
}

.passForm {
    margin: 50px auto;
    width: 40%;

    .passBtn {
        .btn {
            width: 100px;
        }

        .el-form-item__content {
            display: flex;
            justify-content: center;
        }
    }

    ::v-deep .el-form-item__content {
        display: flex;
    }
}

.red-color {
    ::v-deep .el-input__inner {
        color: red;
    }
}
</style>